﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using static UnityEngine.EventSystems.ExecuteEvents;
using XLua;

public class GuidePostEvent : MonoBehaviour, IPointerClickHandler
{
	static int PointerDown = 1;
	static int PointerUp = 2;
	static int PointerClick = 3;
	static int PointerDrag = 4;
	public LuaFunction luaFunc = null;

	public void SetCallback(LuaFunction luaFunction)
	{
		luaFunc = luaFunction;
	}
	public void RaycastThrough<T>(BaseEventData data, EventFunction<T> function) where T : IEventSystemHandler
	{
		var pointerData = data as PointerEventData;
		List<RaycastResult> results = new List<RaycastResult>();
		EventSystem.current.RaycastAll(pointerData, results);
		GameObject current = pointerData.pointerCurrentRaycast.gameObject;
		for (int i = 0; i < results.Count; ++i)
		{
			if (current != results[i].gameObject)
			{
				ExecuteEvents.Execute(results[i].gameObject, data, function);
                break;
            }
		}
	}

	public void OnPointerClick(PointerEventData eventData)
	{
		if (luaFunc != null)
			luaFunc.Call(PointerClick, eventData);
		RaycastThrough(eventData, ExecuteEvents.pointerClickHandler);
	}
}